// Fill out your copyright notice in the Description page of Project Settings.


#include "UI/HallUI/UMG/UI_MainHall.h"

#include "UI_ArchivesSystem.h"
#include "UI_GameSettingsSystem.h"
#include "UI_TutorialWebsiteSystem.h"
#include "Components/SizeBox.h"
#include "UI/HallUI/UMG/UI_HallMenuSystem.h"

template<class T, class UserObject>
UserObject* CreateAssistWidget(T* ThisClass, UClass* AssistClass, USizeBox* WidgetArray)
{
	UserObject* UserObjectElement = nullptr;

	// 播放UI动画
	if (0)
	{
		// todo 淡入动画
	}

	// 获取第一个子元素（判断是否存在子元素）
	if (WidgetArray->GetChildAt(0))
	{
		// 子元素是否为当前类
		if (WidgetArray->GetChildAt(0)->IsA(AssistClass))
		{
			// 关闭 Border
			return UserObjectElement;
		}
		else
		{
			// 清除所有子元素
			WidgetArray->ClearChildren();
		}
	}

	UserObjectElement = CreateWidget<UserObject>(ThisClass->GetWorld(), AssistClass);
	if (UserObjectElement)
	{
		if (WidgetArray->AddChild(UserObjectElement))
		{
			
		}
		else
		{
			UserObjectElement->RemoveFromParent();
		}
	}
	return UserObjectElement;
}


void UUI_MainHall::NativeConstruct()
{
	Super::NativeConstruct();
	HallMenuSystem->SetOwner(this);
	
	// 视频中绑定菜单的方案，视频出处：第二章，第十结 12 分左右。
	// {
	// 	FOnButtonClickedEvent Delegate;
	// 	Delegate.AddDynamic(this, &UUI_MainHall::GameStart);
	// 	HallMenuSystem->BindGameStart(Delegate);
	// }
}

void UUI_MainHall::GameStart()
{
}

void UUI_MainHall::SecretTerritory()
{
	
}

void UUI_MainHall::History()
{
	CreateAssistWidget<UUI_MainHall, UUI_ArchivesSystem>(this, ArchivesSystemClass, BoxList);
}

void UUI_MainHall::GameSettings()
{
	CreateAssistWidget<UUI_MainHall, UUI_GameSettingsSystem>(this, GameSettingsSystemClass, BoxList);
}

void UUI_MainHall::TutorialWebsite()
{
	CreateAssistWidget<UUI_MainHall, UUI_TutorialWebsiteSystem>(this, TutorialSystemClass, BoxList);
}

void UUI_MainHall::Browser()
{
}

void UUI_MainHall::SpecialContent()
{
}

void UUI_MainHall::QuiteGame()
{
}
